﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SIA.HwAccess;
using SIA.Configurators;
using SIA.HwAccess.PLCSiemens.ConnectionDriver;

namespace SIA.HwAccess.PLCSiemens
{
    public class S7PLC : HwDevice
    {
        public S7PLC(IDictionaryProperty PLCProperty, IDictionaryProperty ConnetionsProperty, IDictionaryProperty SignalsProperty)
        {
            this._name = PLCProperty["NOME"].PropertyValue;

            foreach (IDictionaryProperty dP in ConnetionsProperty.Values)           
                if (dP["DEVICE"].PropertyValue == this._name)
                    switch (dP["PROTOCOLLO"].PropertyValue.ToUpper())
                    {
                        case "FETCHPASSIVO":
                            Connections.Add(dP["NOME"].PropertyValue, new TCPFetchConnection(dP, SignalsProperty));
                            break;
                        case "WRITEPASSIVO":
                            Connections.Add(dP["NOME"].PropertyValue, new TCPWriteConnection(dP, SignalsProperty));
                            break;
                        case "FETCHWRITEPASSIVO":
                            Connections.Add(dP["NOME"].PropertyValue, new TCPFetchWriteConnection(dP, SignalsProperty,this));
                            break;
                        case "TCPOPEN":
                            Connections.Add(dP["NOME"].PropertyValue, new TCPOpenConnection(dP, SignalsProperty));
                            break;
                        default:
                            SIA.Messages.MessageManager.sendMessage(this, "Protocollo '" + dP["PROTOCOLLO"].PropertyValue + "' non supportato", SIA.Messages.eMessageType.WARNING, true);
                            break;
                            //throw new Exception("Protocollo '" + dP["PROTOCOLLO"].PropertyValue + "' non supportato");
                    }
        }
    }
}
